package cn.tedu.bonfire.front.payment.controller;

import cn.tedu.bonfire.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.bonfire.front.payment.pojo.entity.AliPayBean;
import cn.tedu.bonfire.front.payment.pojo.entity.Order;
import cn.tedu.bonfire.front.payment.pojo.param.AliPayParam;
import cn.tedu.bonfire.front.payment.service.IOrderService;
import cn.tedu.bonfire.front.payment.service.IPayModelService;
import cn.tedu.bonfire.front.payment.service.IUserService;
import com.alipay.api.AlipayApiException;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

@RestController
@Slf4j
@Api(tags = "6.阿里支付模块")
@RequestMapping("/alipay")
public class AlipayController {

    private final String CHARSET = "UTF-8";

    @Autowired
    private IPayModelService payModelService;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IUserService userService;

    @PostMapping("/toPay")
    @ApiOperation("支付模块")
    @ApiOperationSupport(order = 100)
    public String alipay(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal, AliPayParam aliPayParam){

        try {
            return payModelService.aliPay(currentPrincipal.getId(), aliPayParam);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @RequestMapping("/returnUrl")
    @ApiOperation("支付返回")
    @ResponseBody
    public void returnUrlMethod(@RequestParam Map<String,String> params, @ApiIgnore HttpServletResponse response) throws AlipayApiException, IOException {
        log.info("开始处理异步通知请求");
        int state = 0;
        //1.验证证书上的公钥是否一致
        boolean signVerified = payModelService.aliSignature(params);
        if (!signVerified) {
            state = 1;
        }

        //2.商户发送的订单号是否一致
        String out_trade_no=params.get("out_trade_no");
        Order order = orderService.getByTradeNo(out_trade_no);
        if(order==null){
            state = 1;
        }

        //3.验证金额是否一致
        String total_amount=params.get("total_amount");
        String totalPrice = order.getTotalPrice();
        if (!totalPrice.equals(total_amount)) {
            state = 1;
        }

        //4.验证状态是否一致
        String trade_status=params.get("trade_status");
        if (!"TRADE_SUCCESS".equals(trade_status)) {
            state = 1;
        }

        if (state==1) {


            userService.payment(order.getTotalPrice(),order.getBuyerId());

            payModelService.updateOrderStatus(out_trade_no);


            response.sendRedirect("http://121.41.86.79:8080/personal/tags");

        }
    }
}
